import { toHomeFollow } from "./to/toHomeFollow"
import { uploadJson } from "./utils/uploadXml"

export class SkerFloaty {
    static floaty: AutoJs.FloatyWindow
    static root: string = files.cwd()
    static get ASSETS() {
        return {
            logo: `file://${files.join(this.root, "assets/logo.png")}`,
            info: `file://${files.join(this.root, "assets/info.png")}`,
            start: `file://${files.join(this.root, "assets/start.png")}`,
            kill: `file://${files.join(this.root, "assets/kill.png")}`,
            bug: `file://${files.join(this.root, "assets/bug.png")}`,
            update: `file://${files.join(this.root, "assets/update.png")}`,
            dev: `file://${files.join(this.root, "assets/dev.png")}`,
            stop: `file://${files.join(this.root, "assets/stop.png")}`,
        }
    }
    static createFloaty() {
        this.floaty = floaty.window(`<LinearLayout layout_width="wrap_content" layout_height="wrap_content" orientation="vertical">
        <com.makeramen.roundedimageview.RoundedImageView alpha="0.6" id="icon" riv_corner_radius="16dp" riv_mutate_background="true" riv_oval="true" w="42dp" h="42dp" padding="4dp" src="${this.ASSETS.logo}"/>
        <LinearLayout id="menu" visibility="gone" background="#ffffff" gravity="left|top" orientation="vertical" clickable="false" layout_width="wrap_content" layout_height="wrap_content">
            <com.makeramen.roundedimageview.RoundedImageView id="info" w="42dp" h="42dp" layout_gravity="center" src="${this.ASSETS.info}" padding="8dp" foreground="?selectableItemBackgroundBorderless"/>
            <com.makeramen.roundedimageview.RoundedImageView id="start" w="42dp" h="42dp" layout_gravity="center" src="${this.ASSETS.start}" padding="8dp" foreground="?selectableItemBackgroundBorderless"/>
            <com.makeramen.roundedimageview.RoundedImageView id="kill" w="42dp" h="42dp" layout_gravity="center" src="${this.ASSETS.kill}" padding="8dp" foreground="?selectableItemBackgroundBorderless"/>
            <com.makeramen.roundedimageview.RoundedImageView id="bug" w="42dp" h="42dp" layout_gravity="center" src="${this.ASSETS.bug}" padding="8dp" foreground="?selectableItemBackgroundBorderless"/>
            <com.makeramen.roundedimageview.RoundedImageView id="update" w="42dp" h="42dp" layout_gravity="center" src="${this.ASSETS.update}" padding="8dp" foreground="?selectableItemBackgroundBorderless"/>
        </LinearLayout>
    </LinearLayout>`)
        ui.run(() => {
            this.floaty.setPosition(device.width - this.floaty.icon.getWidth() * 2 / 3, device.height / 2 - 300)
            const mActionViewIcon = this.floaty.icon;
            const mActionMenu = this.floaty.menu;
            const infoView = mActionMenu.info;
            const startView = mActionMenu.start;
            const killView = mActionMenu.kill;
            const bugView = mActionMenu.bug;
            const updateView = mActionMenu.update;
            let mActionMenuOpen: boolean = false;
            let isStart: boolean = false;
            events.on('stop', () => {
                ui.run(() => {
                    isStart = false;
                    startView?.attr('src', this.ASSETS.start)
                })
            })
            events.on('click', (type: string) => {
                if (type === 'info') {
                    events.emit('info', true)
                }
                if (type === 'logo') {
                    ui.run(() => {
                        mActionMenuOpen = !mActionMenuOpen;
                        mActionViewIcon.attr(`alpha`, mActionMenuOpen ? `1` : `0.6`)
                        mActionMenu.attr('visibility', mActionMenuOpen ? `visible` : `gone`)
                        if (mActionMenuOpen) {
                            this.floaty.setPosition(device.width - this.floaty.icon.getWidth(), device.height / 2 - 300)
                        } else {
                            this.floaty.setPosition(device.width - this.floaty.icon.getWidth() * 2 / 3, device.height / 2 - 300)
                        }
                    })
                }
                if (type === 'start') {
                    ui.run(() => {
                        if (isStart) {
                            isStart = false;
                            startView?.attr('src', this.ASSETS.start)
                        } else {
                            isStart = true;
                            startView?.attr('src', this.ASSETS.stop)
                        }
                        events.emit('start', isStart)
                    })
                }
                if (type === 'kill') {
                    engines.stopAll()
                }
                if (type === 'bug') {
                    this.uploadXml(`debug`, `debug`)
                }
                if (type === 'udpate') {
                    this.update()
                }
            })
            mActionViewIcon.on('click', () => events.emit('click', 'logo'))
            bugView.on('click', () => events.emit('click', 'bug'))
            infoView.on('click', () => events.emit('click', 'info'))
            startView.on('click', () => events.emit('click', 'start'))
            killView.on('click', () => events.emit('click', 'kill'))
            updateView.on('click', () => events.emit('click', 'udpate'))
        })
    }

    static uploadXml(title: string, name: string) {
        threads.start(() => {
            this.log(`问题正在上传`)
            uploadJson(title, name)
            sleep(1000)
            this.log(`问题上传成功`)
        })
    }
    static log(msg: string) {
        events.emit('log', msg)
    }
    static update() {
        engines.execScriptFile(files.join(files.cwd(), 'demo.js'))
    }
}

